package com.baomidou.springmvc.exception.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.springmvc.common.result.ResponseCodeEnum;
import com.baomidou.springmvc.common.result.ServerResponse;
import com.baomidou.springmvc.common.utils.StackTraceUtil;
import com.baomidou.springmvc.exception.myexception.ServiceException;


@ControllerAdvice
public class GlobalExceptionHandler {

	private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	@ExceptionHandler(ServiceException.class)
	@ResponseBody
	public  ServerResponse<?> serviceExecptionHandler(ServiceException ex) {
		LOGGER.info("业务错误。code：{} msg: {}", ex.getCode(), ex.getMsg());
		return ServerResponse.createByErrorCodeMessage(ex.getCode(), ex.getMsg());
	}
    
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public  ServerResponse<?> errorHandler(Exception ex) {
    	LOGGER.error("系统错误。msg: {}", StackTraceUtil.getStackTraceInfo(ex));
        //return ServerResponse.createByError();
        return ServerResponse.createByErrorCodeMessage(ResponseCodeEnum.ERROR.getCode(), StackTraceUtil.getStackTraceInfo(ex));
    }
}
